/*-------------------------------------------------------------------------
 * help_config.c
 *
 * 显示大统一配置方案下可用的选项
 *
 * 标记位被设置为GUC_NO_SHOW_ALL、GUC_NOT_IN_SAMPLE或GUC_DISALLOW_IN_FILE的选项不会显示，除非用户特意按名称请求该变量。
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *	  src/backend/utils/misc/help_config.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include <limits.h>
#include <unistd.h>

#include "utils/guc_tables.h"
#include "utils/help_config.h"


/*
 * 这个联合允许我们混合组织的各种不同类型的结构体
 */
typedef union
{
	struct config_generic generic;
	struct config_bool _bool;
	struct config_real real;
	struct config_int integer;
	struct config_string string;
	struct config_enum _enum;
} mixedStruct;


static void fc_printMixedStruct(mixedStruct *fc_structToPrint);
static bool fc_displayStruct(mixedStruct *fc_structToDisplay);


void GucInfoMain(void)
{
	struct config_generic **fc_guc_vars;
	int			fc_numOpts,
				fc_i;

	/* 初始化 guc_variables[] 数组 */
	build_guc_variables();

	fc_guc_vars = get_guc_variables();
	fc_numOpts = GetNumConfigOptions();

	for (fc_i = 0; fc_i < fc_numOpts; fc_i++)
	{
		mixedStruct *fc_var = (mixedStruct *) fc_guc_vars[fc_i];

		if (fc_displayStruct(fc_var))
			fc_printMixedStruct(fc_var);
	}

	exit(0);
}


/*
 * 如果传递给它的结构体应该显示给用户，
 * 则此函数将返回真。
 */
static bool fc_displayStruct(mixedStruct *fc_structToDisplay)
{
	return !(fc_structToDisplay->generic.flags & (GUC_NO_SHOW_ALL |
											   GUC_NOT_IN_SAMPLE |
											   GUC_DISALLOW_IN_FILE));
}


/*
 * 此函数打印传递给它的通用结构体。它将根据用户想要看到的内容打印出
 * 不同的格式。
 */
static void fc_printMixedStruct(mixedStruct *fc_structToPrint)
{
	printf("%s\t%s\t%s\t",
		   fc_structToPrint->generic.name,
		   GucContext_Names[fc_structToPrint->generic.context],
		   _(config_group_names[fc_structToPrint->generic.group]));

	switch (fc_structToPrint->generic.vartype)
	{

		case PGC_BOOL:
			printf("BOOLEAN\t%s\t\t\t",
				   (fc_structToPrint->_bool.reset_val == 0) ?
				   "FALSE" : "TRUE");
			break;

		case PGC_INT:
			printf("INTEGER\t%d\t%d\t%d\t",
				   fc_structToPrint->integer.reset_val,
				   fc_structToPrint->integer.min,
				   fc_structToPrint->integer.max);
			break;

		case PGC_REAL:
			printf("REAL\t%g\t%g\t%g\t",
				   fc_structToPrint->real.reset_val,
				   fc_structToPrint->real.min,
				   fc_structToPrint->real.max);
			break;

		case PGC_STRING:
			printf("STRING\t%s\t\t\t",
				   fc_structToPrint->string.boot_val ? fc_structToPrint->string.boot_val : "");
			break;

		case PGC_ENUM:
			printf("ENUM\t%s\t\t\t",
				   config_enum_lookup_by_value(&fc_structToPrint->_enum,
											   fc_structToPrint->_enum.boot_val));
			break;

		default:
			write_stderr("internal error: unrecognized run-time parameter type\n");
			break;
	}

	printf("%s\t%s\n",
		   (fc_structToPrint->generic.short_desc == NULL) ? "" : _(fc_structToPrint->generic.short_desc),
		   (fc_structToPrint->generic.long_desc == NULL) ? "" : _(fc_structToPrint->generic.long_desc));
}
